#!/bin/bash
SITE=$1


mkdir -p /etc/systemd/system/containerd.service.d
if [ -f /etc/systemd/system/containerd.service.d/http-proxy.conf ]; then
    rm /etc/systemd/system/containerd.service.d/http-proxy.conf
fi

cat <<EOF | tee /etc/systemd/system/containerd.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=$SITE"
Environment="HTTPS_PROXY=$SITE"
Environment="NO_PROXY=docker.m.daocloud.io,hub.cangling.cn,localhost,127.0.0.1,10.10.10.0/24,10.241.0.0/16,192.168.1.0/24,10.96.0.0/12"
EOF

mkdir -p /etc/systemd/system/docker.service.d
if [ -f /etc/systemd/system/docker.service.d/http-proxy.conf ]; then
    rm /etc/systemd/system/docker.service.d/http-proxy.conf
fi
cat <<EOF | tee /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=$SITE"
Environment="HTTPS_PROXY=$SITE"
Environment="NO_PROXY=docker.m.daocloud.io,hub.cangling.cn,localhost,127.0.0.1,10.10.10.0/24,10.241.0.0/16,192.168.1.0/24,10.96.0.0/12"
EOF



systemctl daemon-reload
systemctl restart containerd

# check if docker service exist
systemctl list-unit-files --type=service | grep -q "docker.service"
if [ $? -eq 0 ]; then
    systemctl restart docker
fi
